﻿#include "clearpathDB.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QCoreApplication>
#include <QDateTime>

ClearPathDB::ClearPathDB()
{
    dbPath = QCoreApplication::applicationDirPath()+"/ClearPathDataBse.db";
    dataBase = QSqlDatabase::addDatabase("QSQLITE",QString("connect_%1").arg(QDateTime::currentDateTime().toString("hhmmsszzz")));
    openDataBase();
}

ClearPathDB::~ClearPathDB()
{
    closeDataBase();
}

QSqlDatabase ClearPathDB::getDataBase() const
{
    return dataBase;
}

QList<ClearPath> ClearPathDB::getClearPathList()
{
    QList<ClearPath> list;
    QSqlQuery sql_query(dataBase);
    int res = -1;
    //全等查询
    if(sql_query.exec(QString("select * from ClearPath_table"))){
        while (sql_query.next())
        {
            ClearPath clearPath;
            clearPath.id = sql_query.value(0).toInt();
            clearPath.setPath(sql_query.value(1).toString().toStdString());
            clearPath.setFilterSuffix(sql_query.value(2).toString().toStdString());
            clearPath.cascade = sql_query.value(3).toBool();
            clearPath.data_number_enable = sql_query.value(4).toBool();
            clearPath.data_number = sql_query.value(5).toInt();
            clearPath.unit_option = sql_query.value(6).toInt();
            clearPath.time_enable = sql_query.value(7).toBool();
            clearPath.elapsed = sql_query.value(8).toInt();
            clearPath.time_option = sql_query.value(9).toInt();
            clearPath.enable = sql_query.value(10).toBool();
            list.append(clearPath);
        }
    }else{
        qCritical() << sql_query.lastError();
    }
    return list;
}

void ClearPathDB::addClearPath(ClearPath clearPath)
{
    QSqlQuery sql_query(dataBase);
    QString sqlStr = QString("insert into ClearPath_table(path,filter_suffix,cascade,elapsed,data_number_enable,data_number,unit_option,time_enable,time_option,enable) values('%1','%2',%3,%4,%5,%6,%7,%8,%9,%10)")
            .arg(clearPath.path)
            .arg(clearPath.filter_suffix)
            .arg(clearPath.cascade)
            .arg(clearPath.data_number_enable)
            .arg(clearPath.data_number)
            .arg(clearPath.unit_option)
            .arg(clearPath.time_enable)
            .arg(clearPath.elapsed)
            .arg(clearPath.time_option)
            .arg(clearPath.enable);
    if(sql_query.exec(sqlStr)){

    }else{
        qCritical() << sql_query.lastError();
    }

}

void ClearPathDB::upClearPath(ClearPath clearPath)
{
    QSqlQuery sql_query(dataBase);
    QString sqlStr = QString("update ClearPath_table set "
                             "path = '%1', "
                             "filter_suffix = '%2', "
                             "cascade = %3, "
                             "elapsed = %4, "
                             "data_number_enable = %5, "
                             "data_number = %6, "
                             "unit_option = %7, "
                             "time_enable = %8, "
                             "time_option = %9,"
                             "enable = %10 "
                             "where id=%11")

            .arg(clearPath.path)
            .arg(clearPath.filter_suffix)
            .arg(clearPath.cascade)
            .arg(clearPath.elapsed)
            .arg(clearPath.data_number_enable)
            .arg(clearPath.data_number)
            .arg(clearPath.unit_option)
            .arg(clearPath.time_enable)
            .arg(clearPath.time_option)
            .arg(clearPath.enable)
            .arg(clearPath.id);
    if(sql_query.exec(sqlStr)){

    }else{
        qCritical() << sql_query.lastError();
    }
}

void ClearPathDB::openDataBase()
{
    dataBase.setDatabaseName(dbPath);
    dataBase.open();
    QSqlQuery sql_query(dataBase);
    if(!sql_query.exec("select * from ClearPath_table"))
        if(!sql_query.exec("create table ClearPath_table("
                           "id integer primary key,"
                           "path text,"
                           "filter_suffix text,"
                           "cascade bool,"
                           "data_number_enable bool,"
                           "data_number integer,"
                           "unit_option integer,"
                           "time_enable bool,"
                           "elapsed integer,"
                           "time_option integer,"
                           "enable bool)"))
            qCritical() << sql_query.lastError();
}

void ClearPathDB::closeDataBase()
{
    if(dataBase.isOpen())
        dataBase.close();
}
